<template>
  <div class="box">
    <h2>Son1 子组件</h2>
    从vuex中获取的值: <label>{{ $store.state.count }}</label>
    <br />
    <button @click="handleAdd(1)">值 + 1</button>
    <button @click="handleAdd(5)">值 + 5</button>
    <button @click="handleAdd(10)">值 + 10</button>
    <button @click="changeFn">改标题</button>
    <button @click="handleChange">一秒后修改成666</button>
    <button @click="changeCountAction(200)">一秒后修改成200</button>
    <hr />
    <!-- 计算属性getters -->
    <div>{{ $store.state.list }}</div>
    <div>{{ $store.getters.filterList }}</div>
    <div>{{ filterList }}</div>
    <hr />

    <!-- 测试访问模块中的state - 原生 -->
    <div>{{ $store.state.user.userInfo.name }}</div>
    <div>{{ $store.state.setting.desc }}</div>
    <div>user模块的数据：{{ userInfo }}</div>
    <div>setting模块下的数据：{{ desc }}</div>

    <!-- 测试访问模块中的getters - 原生 -->
    <div>{{ $store.getters["user/UpperCaseName"] }}</div>
    <div>{{ UpperCaseName }}</div>
    <hr />

    <button @click="updateUser">更新个人信息</button>
    <button @click="updateUser2">一秒后更新信息</button>
    <button @click="setUserSecond({ name: 'xiaoli', age: 80 })">
      一秒后更新信息
    </button>
    <button @click="updateTheme">更新主题色</button>
    <button @click="setUser({ name: 'xiaoli', age: 80 })">更新个人信息</button>
    <button @click="setTheme('skyblue')">更新主题</button>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from "vuex";

export default {
  name: "Son1Com",
  created() {
    console.log(this.$store.getters);
  },
  computed: {
    // mapState 和 mapGetters 都是映射属性
    ...mapState(["count", "title"]),
    ...mapGetters(["filterList"]),
    ...mapState("user", ["userInfo"]),
    ...mapState("setting", ["theme", "desc"]),
    ...mapGetters("user", ["UpperCaseName"]),
  },
  methods: {
    // mapMutations 和 mapActions 都是映射方法
    // 全局级别的映射
    ...mapMutations(["subCount", "changeTitle"]),
    ...mapActions(["changeCountAction"]),
    ...mapMutations("setting", ["setTheme"]),
    ...mapMutations("user", ["setUser"]),
    ...mapActions("user", ["setUserSecond"]),
    handleAdd(n) {
      // 错误代码(vue默认不会监测，监测需要成本)
      // this.$store.state.count++;
      // console.log(this.$store.state.count);

      // 应该通过mutation核心概念，进行修改数据
      // 需要提交调用mutation
      // this.$store.commit("addCount");
      // this.$store.commit("addCount", n);
      this.$store.commit("addCount2", {
        count: n,
      });
    },
    changeFn() {
      this.$store.commit("changeTitle", "传智教育");
    },
    handleChange() {
      this.$store.dispatch("changeCountAction", 666);
    },
    updateUser() {
      // $store.commit('模块名/mutation名', 额外传参)
      this.$store.commit("user/setUser", {
        name: "xiaowang",
        age: 25,
      });
    },
    updateUser2() {
      // 调用action dispatch
      this.$store.dispatch("user/setUserSecond", {
        name: "xiaohong",
        age: 28,
      });
    },
    updateTheme() {
      this.$store.commit("setting/setTheme", "pink");
    },
  },
};
</script>

<style lang="css" scoped>
.box {
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>
